package com.gaohe.web.controller.scp;

import java.util.HashMap;
import java.util.List;

import com.gaohe.scp.domain.CsCar;
import com.gaohe.scp.domain.CsOrderDetail;
import com.gaohe.scp.service.ICsCarService;
import com.gaohe.scp.service.ICsOrderDetailService;

import com.gaohe.scp.domain.vo.CsOrderVo;
import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gaohe.common.annotation.Log;
import com.gaohe.common.core.controller.BaseController;
import com.gaohe.common.core.domain.AjaxResult;
import com.gaohe.common.enums.BusinessType;
import com.gaohe.scp.domain.CsOrder;
import com.gaohe.scp.service.ICsOrderService;
import com.gaohe.common.utils.poi.ExcelUtil;
import com.gaohe.common.core.page.TableDataInfo;

/**
 * 用户收藏Controller
 *
 * @author liuxiangyu
 * @date 2025-03-16
 */
@RestController
@RequestMapping("/scp/order")
public class CsOrderController extends BaseController
{
    @Autowired
    private ICsOrderDetailService csOrderDetailService;
    @Autowired
    private ICsCarService csCarService;
    @Autowired
    private ICsOrderService csOrderService;

    /**
     * 查询用户收藏列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsOrder csOrder)
    {
        startPage();
        List<CsOrder> list = csOrderService.selectCsOrderList(csOrder);
        return getDataTable(list);
    }

    /**
 * 查询订单列表    王浩宇 */
    @GetMapping("/list1")
    public TableDataInfo list1(CsOrder csOrder){
        startPage();
        List<CsOrderVo> list1 = csOrderService.selectOrderList(csOrder);
        return getDataTable(list1);
    }
    /**
     * 导出用户收藏列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:order:export')")
    @Log(title = "用户收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsOrder csOrder)
    {
        List<CsOrder> list = csOrderService.selectCsOrderList(csOrder);
        ExcelUtil<CsOrder> util = new ExcelUtil<CsOrder>(CsOrder.class);
        util.exportExcel(response, list, "用户收藏数据");
    }

    /**
     * 获取用户收藏详细信息
     */
//    @PreAuthorize("@ss.hasPermi('scp:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(csOrderService.selectCsOrderByOrderId(orderId));
    }

    /**
     * 获取用户收藏详细信息 管理端
     */
//    @PreAuthorize("@ss.hasPermi('scp:order:query')")
    @GetMapping(value = "/gld/{orderId}")
    public AjaxResult getInfoGld(@PathVariable("orderId") Long orderId)
    {
        CsOrder csOrder = csOrderService.selectCsOrderByOrderId(orderId);
        CsOrderDetail csOrderDetail = csOrderDetailService.selectCsOrderDetailByorderId(orderId);
        Long carId = csOrderDetail.getCarId();
        CsCar csCar = csCarService.selectCsCarByCarId(carId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("car",csCar);
        map.put("order",csOrder);
        map.put("orderDetail",csOrderDetail);
        return success(map);
    }

    /**
     * 新增用户收藏
     */
//    @PreAuthorize("@ss.hasPermi('scp:order:add')")
    @Log(title = "用户收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsOrder csOrder)
    {
        return toAjax(csOrderService.insertCsOrder(csOrder));
    }

    /**
     * 修改用户收藏
     */
//    @PreAuthorize("@ss.hasPermi('scp:order:edit')")
    @Log(title = "用户收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsOrder csOrder)
    {
        return toAjax(csOrderService.updateCsOrder(csOrder));
    }

    /**
     * 删除用户收藏
     */
//    @PreAuthorize("@ss.hasPermi('scp:order:remove')")
    @Log(title = "用户收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(csOrderService.deleteCsOrderByOrderIds(orderIds));
    }
    //根据用户id查询订单
//    @PreAuthorize("@ss.hasPermi('scp:order:user:query')")
    @GetMapping("/user/{userId}")
    public TableDataInfo getOrderByUserId(@PathVariable Long userId)
    {
        startPage();
        List<CsOrder> list = csOrderService.getOrderByUserId(userId);
        return getDataTable(list);
    }
}
